// task_pkg/pages/map/map.js
import QQMAP from '../../../utils/qqmap'
Page({
  data: {
      latitude: 24.4798,
      longitude: 118.0894,
      scale: 6,
      markers: [
        {
          id: 1,
          latitude: 30.69982,
          longitude: 114.4096,
          width: 20,
          height: 20,
        },
        {
          id: 2,
          latitude: 24.4798,
          longitude: 118.0894,
          width: 20,
          height: 20,
        }],
      polyline: [],
  },
  getPolyline() {
    QQMAP.direction({
      mode: 'driving',
      to: '30.59982,114.3096',
      from: '24.4798,118.0894',
      success: (res) => {
        console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
        console.log(pl)
        //设置polyline属性，将路线显示出来,将解压坐标第一个数据作为起点
        this.setData({
          latitude: pl[0].latitude,
          longitude: pl[0].longitude,
          polyline: [{
            points: pl,
            color: '#FF0000DD',
            width:3
          }]
        })
      }
    })
  },

  onLoad() {
this.getPolyline()
  },

})